//
// Created by ins on 2021/3/6.
//

#include "BossDao.h"
#include "BossBean.h"
#include <iostream>
#include <string>
#include <kisql.h>

using std::string;

BossDao *BossDao::sInstance = new BossDao();
BossDao::BossDao() {}
BossDao::~BossDao() {}

BossDao* BossDao::getInstance() {
    return sInstance;
}

void BossDao::getBoss(string bossId, BossBean &retBoss) {
    kisql *db = kisql::getInstance();
    string sql = "select * from boss where bossid = " + bossId;

    MYSQL_RES *result;
    MYSQL_ROW row;


    if (mysql_query(db->getMysql(), sql.c_str())) {
        perror("mysql query err");
        return;
    } else { //select success
        result = mysql_store_result(db->getMysql());
        if (result) {
            int num_rows = mysql_num_rows(result);

            for (int i = 0; i < num_rows; i++) {
                row = mysql_fetch_row(result);
                if (row == nullptr) {
                    break;
                }

                retBoss.setId(row[0]);
                retBoss.setName(row[1]);
                retBoss.setInfo(row[2]);
                retBoss.setLv(row[3]);
                retBoss.setHp(row[4]);
                retBoss.setMaxHp(row[5]);
                retBoss.setAttack(row[6]);
                retBoss.setDefence(row[7]);
                retBoss.setBaoji(row[8]);
                retBoss.setXixue(row[9]);
                retBoss.setZb(row[10]);
                retBoss.setDaoju(row[11]);
                retBoss.setBs(row[12]);
                retBoss.setTime(row[13]);

                cout << "bossDao: " << retBoss.toString() << endl;
            }
        }
    }
}